﻿using System.Collections.Generic;
using System.Globalization;
using OA.Manager;
using OA.Model;
using OA.Utility;

namespace OA.Bll.Personnel
{
    public class TrainingPresenter : BasePresenter<TrainingInfo>
    {
        private readonly TrainingManager _manager;

        public TrainingPresenter()
        {
            _manager = new TrainingManager();
        }

        /// <summary>
        /// 只是添加培训信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public override string Add(JsonReq req)
        {
            bool bl = false;
            //EmployeeTrainingMappingManager _Manager = new EmployeeTrainingMappingManager();
            TrainingInfo trainingInfo = JsonHelper.JsonDeserialize<TrainingInfo>(req.Options);
            int trainingId = _manager.InsertTrainingInfo(trainingInfo);
            bl = trainingId > 0;
            //EmployeeTrainingMappingInfo employeeTraining = new EmployeeTrainingMappingInfo();
            //if (trainingId > 0)
            //{
            //    bl = true;
            //    if (!string.IsNullOrEmpty(req.Options["EmployeeId"]))
            //    {
            //        string[] ids = req.Options["EmployeeId"].Split(new char[] {';'});
            //        if (ids.Length > 0)
            //        {
            //            foreach (string s in ids)
            //            {
            //                int id = 0;
            //                int.TryParse(s, out id);
            //                if (id > 0)
            //                {
            //                    employeeTraining.EmployeeId = id;
            //                    employeeTraining.TrainingId = trainingId;
            //                    bl = _Manager.InsertEmployeeTrainingMappingInfo(employeeTraining);
            //                    if (!bl)
            //                        break;
            //                }
            //            }
            //        }
            //    }
            //}
            return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        }

        //public override string Add(JsonReq req)
        //{
        //    bool bl = false;
        //    EmployeeTrainingMappingManager _Manager = new EmployeeTrainingMappingManager();
        //    TrainingInfo trainingInfo = JsonHelper.JsonDeserialize<TrainingInfo>(req.Options);
        //    int trainingId = _manager.InsertTrainingInfo(trainingInfo);
        //    EmployeeTrainingMappingInfo employeeTraining = new EmployeeTrainingMappingInfo();
        //    if (trainingId > 0)
        //    {
        //        bl = true;
        //        if (!string.IsNullOrEmpty(req.Options["EmployeeId"]))
        //        {
        //            string[] ids = req.Options["EmployeeId"].Split(new char[] { ';' });
        //            if (ids.Length > 0)
        //            {
        //                foreach (string s in ids)
        //                {
        //                    int id = 0;
        //                    int.TryParse(s, out id);
        //                    if (id > 0)
        //                    {
        //                        employeeTraining.EmployeeId = id;
        //                        employeeTraining.TrainingId = trainingId;
        //                        bl = _Manager.InsertEmployeeTrainingMappingInfo(employeeTraining);
        //                        if (!bl)
        //                            break;
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        //}

        public override string Update(JsonReq req)
        {
            bool bl = false;
            //EmployeeTrainingMappingManager manager = new EmployeeTrainingMappingManager();
            TrainingInfo trainingInfo = JsonHelper.JsonDeserialize<TrainingInfo>(req.Options);
            List<string> needUpdateFields = new List<string>();
            needUpdateFields.Add("StartTime");
            needUpdateFields.Add("FinishedTime");
            needUpdateFields.Add("TrainingInstitutions");
            needUpdateFields.Add("TrainingCourse");
            needUpdateFields.Add("Certification");
            needUpdateFields.Add("TrainingFee");
            needUpdateFields.Add("ClassHour");
            needUpdateFields.Add("Lecturer");
            needUpdateFields.Add("TrainingType");
            needUpdateFields.Add("CertificationNo");
            bl = _manager.UpdateTrainingInfo(trainingInfo, needUpdateFields);
            //EmployeeTrainingMappingInfo employeeTraining = new EmployeeTrainingMappingInfo();
            //if (bl)
            //{
            //    if (!string.IsNullOrEmpty(req.Options["EmployeeId"]))
            //    {
            //        string[] ids = req.Options["EmployeeId"].Split(new char[] {';'});
            //        if (ids.Length > 0)
            //        {
            //            foreach (string s in ids)
            //            {
            //                int id = 0;
            //                int.TryParse(s, out id);
            //                if (id > 0)
            //                {
            //                    employeeTraining.EmployeeId = id;
            //                    employeeTraining.TrainingId = trainingInfo.Id;
            //                    bl = manager.DeleteEmployeeTitleInfo("TrainingId='" + trainingInfo.Id.ToString(
            //                        CultureInfo.InvariantCulture) + "'");
            //                    if (bl)
            //                    {
            //                        bl = manager.InsertEmployeeTrainingMappingInfo(employeeTraining);
            //                    }
            //                    if (!bl)
            //                        break;
            //                }
            //            }
            //        }
            //    }
            //}
            return JsonHelper.ResultToJson(bl, ExecuteType.Update);
        }

        //public override string Update(JsonReq req)
        //{
        //    bool bl = false;
        //    EmployeeTrainingMappingManager manager = new EmployeeTrainingMappingManager();
        //    TrainingInfo trainingInfo = JsonHelper.JsonDeserialize<TrainingInfo>(req.Options);
        //    List<string> needUpdateFields = new List<string>();
        //    needUpdateFields.Add("StartTime");
        //    needUpdateFields.Add("FinishedTime");
        //    needUpdateFields.Add("TrainingInstitutions");
        //    needUpdateFields.Add("TrainingCourse");
        //    needUpdateFields.Add("Certification");
        //    needUpdateFields.Add("TrainingFee");
        //    needUpdateFields.Add("ClassHour");
        //    needUpdateFields.Add("Lecturer");
        //    bl = _manager.UpdateTrainingInfo(trainingInfo, needUpdateFields);
        //    EmployeeTrainingMappingInfo employeeTraining = new EmployeeTrainingMappingInfo();
        //    if (bl)
        //    {
        //        if (!string.IsNullOrEmpty(req.Options["EmployeeId"]))
        //        {
        //            string[] ids = req.Options["EmployeeId"].Split(new char[] { ';' });
        //            if (ids.Length > 0)
        //            {
        //                foreach (string s in ids)
        //                {
        //                    int id = 0;
        //                    int.TryParse(s, out id);
        //                    if (id > 0)
        //                    {
        //                        employeeTraining.EmployeeId = id;
        //                        employeeTraining.TrainingId = trainingInfo.Id;
        //                        bl = manager.DeleteEmployeeTitleInfo("TrainingId='" + trainingInfo.Id.ToString(
        //                            CultureInfo.InvariantCulture) + "'");
        //                        if (bl)
        //                        {
        //                            bl = manager.InsertEmployeeTrainingMappingInfo(employeeTraining);
        //                        }
        //                        if (!bl)
        //                            break;
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        //}
    }
}
